﻿package org.electric.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.electric.bean.productBean;
import org.electric.service.CompanyService;
import org.electric.util.WriterUtil;

/**
 * Servlet implementation class productDisplay
 */
@WebServlet("/productDisplay")
public class productDisplay extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public productDisplay() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-Type","text/html;charset=utf-8" );
		CompanyService cs=new CompanyService();
		String pagenub=request.getParameter("number");
		//page表示前端页面显示的最大页码数；
		int page=0;
		if(pagenub!=null&&!"".equals(pagenub)){
			int numb=Integer.parseInt(pagenub);
			//获取产品总数量
			try {
				int count=cs.getproductcount();
				
				if(count%9==0){
					page=count/9;
				}else if(count%9!=0){
					page=count/9+1;
				}
				//获取数据库所以产品的list集合
				List<productBean> list=cs.productDisplay(numb);
				
				ObjectMapper om= WriterUtil.writeTopage();
				HashMap<String, Object> map= new HashMap<String, Object>();
				map.put("list", list);
				map.put("countall", page);
				System.out.println(map);
				om.writeValue(response.getWriter(),map);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			System.out.println("11111111111111");
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
